﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Supermarket;
using TencentCloud.Common;
using TencentCloud.Common.Profile;
using TencentCloud.Sms.V20210111;
using TencentCloud.Sms.V20210111.Models;

namespace XLSupermarket.Account.ChangePassword
{
    public partial class UpPwd : Form
    {
        public UpPwd()
        {
            InitializeComponent();
        }
        string VerifyCode = "``````";
        string TUser = LoginForm.UserName_Value;
        /// <summary>
        /// 加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UpPwd_Load(object sender, EventArgs e)
        {
            comboBox1.SelectedIndex = 0;
            Text_Two.UseSystemPasswordChar = true;
            Text_New.UseSystemPasswordChar = true;
        }
        /// <summary>
        /// 修改方式判断
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            String TType = comboBox1.Text;//修改方式
            //对验证方式进行判断
            if (TType == "原密码")
            {
                Text_pwd.UseSystemPasswordChar = true;
                this.Text_pwd.Size = new System.Drawing.Size(123, 26);
                pictureBox2.Show();
                BT_Obtain.Hide();
                LbYuan.Text = "请输入原密码";
                label2.Text = "原密码：";
            }
            else if (TType == "身份证")
            {
                Text_pwd.UseSystemPasswordChar = true;
                Text_pwd.Size =new Size(123, 26);
                pictureBox2.Show();
                BT_Obtain.Hide();
                LbYuan.Text = "请输入身份证后6位";
                label2.Text = "身份证：";
            }
            else if (TType == "验证码")
            {
                Text_pwd.UseSystemPasswordChar = false;
                this.Text_pwd.Size = new System.Drawing.Size(100, 26);
                pictureBox2.Hide();
                BT_Obtain.Show();
                LbYuan.Text = "点击获取来取得验证码";
                label2.Text = "验证码";
            }
        }
        /// <summary>
        /// 单机取消按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BT_Cenel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        /// <summary>
        /// 修改密码功能
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BT_Yes_Click(object sender, EventArgs e)
        {
            String sql_user = "select UserName from EP_Number EN where EN.UserName='" + LoginForm.UserName_Value + "'";
            DataTable dt_user = DBHelper.FillData(sql_user);
            String TUser = dt_user.Rows[0][0].ToString();//用户名
            String TType = comboBox1.Text;//修改方式
            String TPwd = Text_pwd.Text;//原密码/身份证/手机号
            String TNew = Text_New.Text;//新密码
            String TTwo = Text_Two.Text;//新密码第二次输入
            //文本框检测
            {
                //判断用户名是否不等于登录的用户名
                if (TUser !=LoginForm.UserName_Value)
                {
                    MessageBox.Show("您只能修改当前登录用户的密码！","警告",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                    return;
                }
                //原密码/身份证/手机号
                if (TPwd == null || TPwd.Equals(""))
                {
                    LbYuan.ForeColor = Color.Red;
                    return;
                }
                else LbYuan.ForeColor = Color.Black;
                //新密码
                if (TNew == null || TNew.Equals(""))
                {
                    LbNew.ForeColor = Color.Red;
                    return;
                }
                else LbNew.ForeColor = Color.Black;
                //新密码第二次
                if (TTwo == null || TTwo.Equals(""))
                {
                    LbNew2.ForeColor = Color.Red;
                    return;
                }
                else LbNew2.ForeColor = Color.Black;
                //判断密码两次是否相同
                if (TNew!= TTwo)
                {
                    MessageBox.Show("您的第一次密码和第二次密码不一样","提示");
                    return;
                }
            }
            //信息对比
            {
                string sql_sl = string.Format("SELECT EI.EP_ID id,SUBSTRING(EP_IDcard,13,6) card,EP_Phone phone,EN.UserName uname,EN.Password pwd FROM EP_information EI,EP_Number EN where EI.EP_ID = EN.EP_ID and UserName='{0}'", TUser);
                DataTable dt = DBHelper.FillData(sql_sl);
                if (dt.Rows.Count>0&& TUser== dt.Rows[0]["uname"].ToString())
                {
                    switch (TType)
                    {
                        case "原密码":
                            String pwd = dt.Rows[0]["pwd"].ToString();//取原密码
                            if (TPwd!=pwd)
                            {
                                MessageBox.Show("原密码输入错误！");
                                return;
                            }
                            break;
                        case "身份证":
                            String idcard = dt.Rows[0]["card"].ToString();//取身份证
                            if (TPwd != idcard)
                            {
                                MessageBox.Show("身份证输入错误！");
                                return;
                            }
                            break;
                        case "验证码":
                            if (TPwd != VerifyCode)
                            {
                                MessageBox.Show("验证码输入错误！");
                                return;
                            }
                            break;
                        default:
                            MessageBox.Show("您的修改方式选择有误！");
                            return;
                    }
                }
                else
                {
                    MessageBox.Show("没有查询到该用户名");
                    return;
                }
            }
            String sql_up = string.Format("UPDATE EP_Number SET Password='{0}' where UserName='{1}'", TNew, TUser);
            int result = DBHelper.Edit(sql_up);
            if (result>0)
            {
                MessageBox.Show("修改成功！\n请牢记您的新密码","提示",MessageBoxButtons.OK);
                this.Close();
            }
            else
            {
                MessageBox.Show("密码修改失败！", "提示");
            }   
        }
        /// <summary>
        /// 点击小眼睛会显示密码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pictureBox2_MouseDown(object sender, MouseEventArgs e)
        {
            Text_pwd.UseSystemPasswordChar = false;
        }
        /// <summary>
        /// 松开小眼睛会隐藏密码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pictureBox2_MouseUp(object sender, MouseEventArgs e)
        {
            Text_pwd.UseSystemPasswordChar = true;
        }
        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            Text_New.UseSystemPasswordChar = false;
        }
        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            Text_New.UseSystemPasswordChar = true;
        }
        private void pictureBox3_MouseUp(object sender, MouseEventArgs e)
        {
            Text_Two.UseSystemPasswordChar = true;
        }
        private void pictureBox3_MouseDown(object sender, MouseEventArgs e)
        {
            Text_Two.UseSystemPasswordChar = false;
        }
        /// <summary>
        /// 验证码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BT_Obtain_Click(object sender, EventArgs e)
        {
            try
            {
                string sql_sl = string.Format("SELECT EP_Phone phone FROM EP_information EI,EP_Number EN where EI.EP_ID = EN.EP_ID and UserName='{0}'", TUser);
                DataTable dt = DBHelper.FillData(sql_sl);
                string Phone = dt.Rows[0]["phone"].ToString();
                Credential cred = new Credential
                {
                    SecretId = "AKIDHOEo79uOcZFemez4sPgiTw84j7E4ey1n",
                    SecretKey = "XooX0C7mAH7gy0lFSQ8ZjcFchV3KL4Xj"
                };
                ClientProfile clientProfile = new ClientProfile();
                HttpProfile httpProfile = new HttpProfile();
                httpProfile.Endpoint = ("sms.tencentcloudapi.com");
                clientProfile.HttpProfile = httpProfile;
                SmsClient client = new SmsClient(cred, "ap-beijing", clientProfile);
                SendSmsRequest req = new SendSmsRequest();
                string TEL = "+86";
                TEL += Phone;
                //创建随机数
                Random Rdm = new Random(Guid.NewGuid().GetHashCode());
                //定义一个字符串用来存放6位随机数
                string sRnd = "";
                //for循环产生4个随机数，然后构成一个字符串
                for (int i = 0; i < 6; i++)
                {
                    int iNum = Rdm.Next(0, 10);
                    sRnd += iNum.ToString();
                }
                VerifyCode = sRnd;//验证码
                req.PhoneNumberSet = new string[] { TEL };
                req.SmsSdkAppId = "1400535628";
                req.SignName = "SBQ技术分享";
                req.TemplateId = "1004667";
                req.TemplateParamSet = new string[] { VerifyCode };
                req.SessionContext = "123456";
                SendSmsResponse resp = client.SendSmsSync(req);
                MessageBox.Show("验证码发送成功！\n请注意查看手机短信！","提示");
                //MessageBox.Show("验证码发送成功！\n" + AbstractModel.ToJsonString(resp));
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString() + "\n验证码发送失败！");
            }
        }
    }
}
